import React, { useState, Fragment } from 'react';
import { Form, Input, Row, Col, Button, Icon, message, InputNumber } from 'antd';
import UploadOss from '@/components/UploadOss';
import { get as getPath } from 'object-path';
import { FormContainer, RadioForm, SelectForm } from '@/components/UserForm';
import { getTypeList, getDict } from '@/services/components/userform';
import { findCountry } from '@/pages/RecruitManage/PositionManage/part/utils';
import LinkTo from '@/components/LinkTo';

const { TextArea } = Input;
const Conclusion = ({
    form,
    candidate,
    showFileList,
    uploadResource,
    onSetFileResource,
    dictList,
    interviewimage,
    cancelReasonType = ''
}) => {
    const { getFieldDecorator } = form;

    const { offerPostType = 'GroupOfferPostType' } = findCountry(candidate.companyId);
    const [postTypeRemark2, setpostTypeRemark2] = useState('');
    // 职位职级，通过选择后，获取remark
    const getPostType = (v, option) => {
        form.resetFields(['postLevel2']);
        setpostTypeRemark2(getPath(option, 'props.remark', ''));
    };
    const { conclusionDisable = '' } = dictList;

    /**
     * showFileList 需要设置的文件数据，比如说用来展示目前有多少条上传的附件
     * uploadResource    暂时为null，主要作用返回后端用的
     * showFileListReturn   文件信息
     * uploadResourceReturn     需要传给后端的数据
     */
    const changeAllFileList = (showFileListReturn, uploadResourceReturn) => {
        if (uploadResourceReturn.keyNameAndOpt.length <= 1 && showFileListReturn.length <= 1) {
            onSetFileResource(showFileListReturn, uploadResourceReturn);
        } else {
            message.error('只能上传一份附件');
        }
    };

    return (
        <Fragment>
            <div style={{ marginBottom: 30 }}>
                <span className="vv-from-title">面试结果{cancelReasonType ? '（已取消）' : ''}</span>
            </div>
            {cancelReasonType ? (
                <>
                    <Row>
                        <Col xs={{ span: 24 }} sm={{ span: 24 }} md={{ span: 12 }}>
                            <Form.Item label="取消原因">
                                {getFieldDecorator('cancelReason', {
                                    // initialValue: talentEmail,
                                    rules: [
                                        {
                                            required: true,
                                            message: '请选择原因'
                                        }
                                    ]
                                })(
                                    <SelectForm
                                        params={{ code: 'interviewCancelReason' }}
                                        requireParam={['code']}
                                        getData={params =>
                                            getDict(params).then(res => {
                                                if (res && res.code === 10000 && res.data) {
                                                    return res.data || [];
                                                }
                                                return [];
                                            })
                                        }
                                        fieldsName={{
                                            selectValue: 'dictValue',
                                            selectTitle: 'dictName'
                                        }}
                                        // onChange={this.companyChange}
                                        selectProps={{
                                            placeholder: '请选择取消原因',
                                            style: { minWidth: 200 },
                                            disabled: true
                                        }}
                                    />
                                )}
                            </Form.Item>
                        </Col>
                    </Row>

                    <Row>
                        <Col xs={{ span: 24 }} sm={{ span: 24 }} md={{ span: 12 }}>
                            <Form.Item label="备注">
                                {getFieldDecorator('cancelReasonRemark', {
                                    rules: [
                                        {
                                            required: true,
                                            max: 200,
                                            message: '请输入取消原因的说明，不超过200字符'
                                        }
                                    ]
                                })(
                                    <Input.TextArea
                                        rows={5}
                                        placeholder="请输入取消原因的说明，不超过200字符"
                                        disabled
                                    />
                                )}
                            </Form.Item>
                        </Col>
                    </Row>
                </>
            ) : (
                <FormContainer>
                    <Form.Item label="面试得分">
                        <div style={{ display: 'flex' }}>
                            <div style={{ flex: 1, width: 'calc(100% - 90px)' }}>
                                {getFieldDecorator('interviewScore', {
                                    rules: [
                                        {
                                            required: true,
                                            message: '请输入面试得分'
                                        }
                                    ]
                                })(
                                    <InputNumber
                                        style={{ width: '100%' }}
                                        precision={1}
                                        placeholder="请输入面试得分"
                                        disabled={conclusionDisable}
                                        allowClear
                                    />
                                )}
                            </div>
                            <LinkTo
                                disabled={interviewimage === ''}
                                style={{ marginLeft: 10 }}
                                to={interviewimage}
                                target="_blank"
                            >
                                预览面试评分表
                            </LinkTo>
                        </div>
                    </Form.Item>
                    <Form.Item label="面试结果" vvspan={24} labelCol={{ md: 4 }} wrapperCol={{ md: 20 }}>
                        {getFieldDecorator('interviewConclusion', {
                            rules: [
                                {
                                    required: true,
                                    message: '请选择面试结果'
                                }
                            ]
                        })(
                            <RadioForm
                                params={{ dictCode: 'interviewConclusionType' }}
                                requireParam={['dictCode']}
                                getData={params =>
                                    getTypeList(params).then(res => {
                                        if (res && res.code === 10000 && res.data) {
                                            const arr = res.data.filter(
                                                item => !['wait_for_beginning', 'cancel'].includes(item.dictValue)
                                            );
                                            return arr || [];
                                        }
                                        return [];
                                    })
                                }
                                filterItem="无要求"
                                fieldsName={{
                                    radioValue: 'dictValue',
                                    radioTitle: 'dictName'
                                }}
                                radioProps={{
                                    disabled: conclusionDisable
                                }}
                            />
                        )}
                    </Form.Item>
                    {/* 需求：去掉“候选人定级”字段。但如果是历史的面试，查询面试详情时，任然要展示候选人定级字段，如果是之后的面试，则不再展示 */}
                    {(form.getFieldValue('postLevel') || form.getFieldValue('postLevel2')) && (
                        <Form.Item label="候选人定级" required style={{ marginBottom: 0 }}>
                            <Row gutter={15}>
                                <Col md={12} xs={24} sm={24}>
                                    <Form.Item>
                                        {getFieldDecorator('postLevel', {
                                            rules: [
                                                {
                                                    required: true,
                                                    message: '请选择职位'
                                                }
                                            ]
                                        })(
                                            <SelectForm
                                                params={{ code: offerPostType }}
                                                requireParam={['code']}
                                                getData={params =>
                                                    getDict(params).then(res => {
                                                        if (res && res.code === 10000 && res.data) {
                                                            return res.data || [];
                                                        }
                                                        return [];
                                                    })
                                                }
                                                fieldsName={{
                                                    selectValue: 'dictValue',
                                                    selectTitle: 'dictName'
                                                }}
                                                selectProps={{
                                                    placeholder: '请选择职位',
                                                    disabled: conclusionDisable,
                                                    allowClear: true
                                                }}
                                                onChange={getPostType}
                                            />
                                        )}
                                    </Form.Item>
                                </Col>
                                <Col md={12} xs={24} sm={24}>
                                    <Form.Item>
                                        {getFieldDecorator('postLevel2', {
                                            rules: [
                                                {
                                                    required: true,
                                                    message: '请选择职级'
                                                }
                                            ]
                                        })(
                                            <SelectForm
                                                params={{ code: postTypeRemark2 }}
                                                requireParam={['code']}
                                                getData={params =>
                                                    getDict(params).then(res => {
                                                        if (res && res.code === 10000 && res.data) {
                                                            return res.data || [];
                                                        }
                                                        return [];
                                                    })
                                                }
                                                fieldsName={{
                                                    selectValue: 'dictValue',
                                                    selectTitle: 'dictName'
                                                }}
                                                selectProps={{
                                                    placeholder: '请选择职级',
                                                    disabled: conclusionDisable,
                                                    allowClear: true
                                                }}
                                            />
                                        )}
                                    </Form.Item>
                                </Col>
                            </Row>
                        </Form.Item>
                    )}
                    <Form.Item
                        label="面试总结"
                        vvspan={24}
                        labelCol={{ md: 4 }}
                        wrapperCol={{ md: 20 }}
                        help="2-2000个字符"
                    >
                        {getFieldDecorator('interviewSummary', {
                            rules: [
                                {
                                    required: true,
                                    message: '请输入面试注意点'
                                },
                                {
                                    min: 2,
                                    max: 2000,
                                    message: '请输入2~2000个字符'
                                }
                            ]
                        })(
                            <TextArea
                                placeholder="淘汰/建议录用请概述原因"
                                autosize={{ minRows: 3, maxRows: 5 }}
                                disabled={conclusionDisable}
                            />
                        )}
                    </Form.Item>
                    <Form.Item label="附件" vvspan={24} labelCol={{ md: 4 }} wrapperCol={{ md: 20 }}>
                        {getFieldDecorator(
                            'resourceRelation',
                            {}
                        )(
                            <Fragment>
                                <UploadOss
                                    disabled={conclusionDisable}
                                    dragAble={false}
                                    uploadUrl="/api/resource/public/oss/producePutPreSignUrls"
                                    checkFileSize={10}
                                    checkFileType="png、jpg、doc、docx、pdf、xlsx、xls"
                                    categoryCode="interviewFile"
                                    sysCode="OA"
                                    fileList={showFileList}
                                    uploadResource={uploadResource}
                                    setFileList={changeAllFileList}
                                    checkFileTypeMsg="只能上传png、jpg、doc、docx、pdf、xlsx、xls格式的文件！"
                                    checkFileSizeMsg="文件大小不能超过10MB！"
                                >
                                    <Button type="primary" ghost disabled={conclusionDisable}>
                                        <Icon type="upload" />
                                        上传面试评价表附件
                                    </Button>
                                </UploadOss>
                                <div>支持png、jpg、doc、docx、pdf、xlsx、xls上传，大小为10M以内的文件。</div>
                            </Fragment>
                        )}
                    </Form.Item>
                </FormContainer>
            )}
        </Fragment>
    );
};
export default Conclusion;
